<?php

    $conn = mysql_connect('logrono.gisworking.com:3306', 'cms', 'c33m33s3');
    mysql_select_db('cmstest', $conn);

    if (isset($_GET['clear']))
        clearLogs($conn);
	if (isset ($_GET['lang']))
		changeLang($conn,$_GET['lang']);

    echo "
<p>
    <a href=\"?type=2" . (isset($_GET['limit'])? '&limit='.$_GET['limit']:'') . "\" style=\"color: #FF0000;\">critical</a> |
    <a href=\"?type=3" . (isset($_GET['limit'])? '&limit='.$_GET['limit']:'') . "\" style=\"color: #A23434;\">error</a> |
    <a href=\"?type=4" . (isset($_GET['limit'])? '&limit='.$_GET['limit']:'') . "\" style=\"color: #F0ED37;\">warning</a> |
    <a href=\"?type=6" . (isset($_GET['limit'])? '&limit='.$_GET['limit']:'') . "\" style=\"color: #0000FF;\">info</a> |
    <a href=\"?type=7" . (isset($_GET['limit'])? '&limit='.$_GET['limit']:'') . "\" style=\"color: #000000;\">debug</a>
    <a href=\"?clear=true\" style=\"float: right; color: red;\">clear logs</a>
	<a href=\"?lang=1\" style=\"float: right; color: blue;\">Change Lang EN | </a>
	<a href=\"?lang=2\" style=\"float: right; color: blue;\">Change Lang ES | </a>
    <form action=''>
    limit: <input type=\"text\" name=\"limit\" value=\"" . (isset($_GET['limit'])?$_GET['limit']:'50') . "\"/>
    </form>
</p>";
    echo "<hr/>";

    $result = mysql_query("SELECT id, priority, message, timestamp FROM (SELECT id AS id, priority AS priority, message AS message, timestamp AS timestamp FROM `log` l WHERE priority = " . (isset($_GET['type'])?$_GET['type']:10) . " ORDER BY id DESC LIMIT 0," . (isset($_GET['limit'])?$_GET['limit']:50) . ") AS logs ORDER BY id DESC", $conn);

    while($row = mysql_fetch_array($result, MYSQL_ASSOC))
    {
        echo "
<div id='" . $row['id'] . "' style='width: 100%'>
    <p style=\"width: 100%; color: " . colorize($row['priority']) . ";\">
        [" . labelize($row['priority']) . "] (" . $row['id'] . ") ". $row['timestamp'] . "
    </p>
    <pre style=\"width: 100%; color: " . colorize($row['priority']) . "; border-bottom: dotted 1px #666; \">" . $row['message'] . "</pre>
</div>";
    }

    function colorize($errorId)
    {
        switch ($errorId)
        {
            case 2:
                return "#FF0000";
                break;

            case 3:
                return "#A23434";
                break;

            case 4:
                return "#F0ED37";
                break;

            case 6:
                return "#0000FF";
                break;

            case 7:
                return "#000000";
                break;

            default:
                return "#FF44F9";

        }
    }

    function labelize($errorId)
    {
        switch ($errorId)
        {
            case 2:
                return "critical";
                break;

            case 3:
                return "error";
                break;

            case 4:
                return "warning";
                break;

            case 6:
                return "info";
                break;

            case 7:
                return "debug";
                break;

            default:
                return "#FF44F9";

        }
    }

    function clearLogs($conn)
    {
        mysql_query("TRUNCATE `log`", $conn);
    }
	function changeLang ($conn,$lang)
	{
		
		mysql_query("update `site` set id_language =".$lang ,$conn);
	}
?>
